package com.jcnet.bizaia;

import org.apache.http.Header;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.bean.SPUserBean;
import com.jcnet.bizaia.bean.StateBean;
import com.jcnet.bizaia.tools.DataStorageHelper;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class UserEditPasswordActivity extends JCSimpleSherlockActivity implements OnClickListener{

	private EditText password;
	
	private EditText newPassWord;
	
	private EditText repeatPassWord;
	
	private String passwordString;
	
	private String newPassswordsString;
	
	private String repeatPasswordString;
	
	private ProgressHUD progressHUD;
	
	private Context context;
	
	private DataStorageHelper dataStorageHelper;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		super.jcTitle = getString(R.string.userInfo);
		setContentView(R.layout.user_edit_password);
		initActionBar();
		context = this;
		dataStorageHelper = new DataStorageHelper(this, SPUserBean.dataTableNameForUser);
		
		password = (EditText)findViewById(R.id.edPassword);
		
		newPassWord = (EditText)findViewById(R.id.edNewPassword);
		
		repeatPassWord = (EditText)findViewById(R.id.edRepeatPasssword);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.commitEditPasswordButton:
			
			passwordString = password.getText().toString().trim();
			newPassswordsString = newPassWord.getText().toString().trim();
			repeatPasswordString = repeatPassWord.getText().toString().trim();
			
			
			if(TextUtils.isEmpty(passwordString)){
				Toast.makeText(UserEditPasswordActivity.this, getString(R.string.pleaseInputOldPassWorld), 0).show();
				return;
			}
			if(TextUtils.isEmpty(newPassswordsString)){
				Toast.makeText(UserEditPasswordActivity.this, getString(R.string.pleaseInputNewPassWorld), 0).show();
				return;
			}
			if(newPassswordsString.length()<6){
				Toast.makeText(context, getString(R.string.passwordTooshort), 0).show();
				
				return;
			}
			if(TextUtils.isEmpty(repeatPasswordString)){
				Toast.makeText(UserEditPasswordActivity.this, getString(R.string.pleaseInputReplacePassWorld), 0).show();
				return;
			}
			if(!newPassswordsString.equals(repeatPasswordString)){
				Toast.makeText(UserEditPasswordActivity.this, R.string.pwdNotSame, 0).show();
				return;
			}
			
			if(Toolkit.isAvaliable(UserEditPasswordActivity.this)){
				RequestParams params = new RequestParams();
				try {
					params.put("olduserpwd", Toolkit.MD5(passwordString));
					params.put("userpwd", Toolkit.MD5(newPassswordsString));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				params.put("useridNum", dataStorageHelper.getInt(SPUserBean.userIDSP_1, 400));  //用户ID
			
				new AsyncHttpClient().post(App.HTTP_PATH+"use_updatePwd.action?", params, new AsyncHttpResponseHandler(){
					
					@Override
					public void onStart() {
						// TODO Auto-generated method stub
						progressHUD = ProgressHUD.show(context, getString(R.string.commiting), true	, false, null);
						super.onStart();
					}
					
					
					@Override
					public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
						// TODO Auto-generated method stub
						super.onSuccess(arg0, arg1, arg2);
						
						StateBean stateBean = new Gson().fromJson(new String(arg2), new TypeToken<StateBean>(){}.getType());
					
						if(stateBean.getState()==200){
							Toast.makeText(context, getString(R.string.pwdSuccess), 0).show();
							//重新登录  清空以前的登陆信息
							Intent intent = new Intent(context, LoginActivity.class);
							intent.putExtra("firstIn", 100);
							startActivity(intent);
							finish();
							
						}else{
							Toast.makeText(context, stateBean.getMessage()+"", 0).show();
						}
					}
					
					
					@Override
					public void onFinish() {
						// TODO Auto-generated method stub
						super.onFinish();
						if(progressHUD.isShowing()){
							progressHUD.cancel();
						}
					}
					
					@Override
					public void onFailure(int arg0, Header[] arg1, byte[] arg2,
							Throwable arg3) {
						// TODO Auto-generated method stub
						super.onFailure(arg0, arg1, arg2, arg3);
						
						if(progressHUD.isShowing()){
							progressHUD.cancel();
						}
						
						Toast.makeText(context, getText(R.string.serverError), 0).show();
					}
				});
				
				
			}else{
				Toast.makeText(UserEditPasswordActivity.this, R.string.noNetWork, 0).show();
			}
			break;

		default:
			break;
		}
		
	}
}
